Working with AWS DynamoDB
Using Amazon's AmazonDynamoDBClient and DynamoDBMapper classes
The developer guide has many examples for working with AWS's DynamoDB tables.
We will go through the steps outlined in part 8 of this guide (Store and Retreive App Data in Amazon AWS). We will cover how to
access the database and perform writes, updates, and queries from an Amazon AWS DynamoDB cloud database from an Android application.
Step 1: Add AWS Dependencies to Android Project and Allow for Internet Access:
Note: I'm assuming in this explanation that you already have knowledge of and have created the DynamoDB tables in AWS Mobile Hub.
Before we get to working with the classes provided by Amazon, we need to gain access to their jar files.
Including the highlighted dependencies below will do this. Add these dependencies to your app's build.gradle (app) file and build the project
again to ensure everything is set up fine.
In addition, we need to allow internet access for our app so that we can make http requests and get http responses from the AWS servers.
Below, we show the highlighted addition we need to make to our app's manifest.xml file.
Step 2: Imports:
Below are the imports necessary for working with the AmazonDynamoDBClient and DynamoDBMapper classes. Import these as necessary whenever
your app needs these files.
Step 3: Setting up the AmazonDynamoDBClient and DBMapper:
Now we're ready to really get going. First, we need to set up our AmazonDynamoDBClient which we'll use to create a DynamoDBMapper to our
database. To do this, we need to make use of the AWS Cognito Identity Pool set up for your application.
Go to the Amazon AWS Mobile Hub, then select the resources tab on the left-hand taskbar. From the resources page, go to Amazon Cognito Identity Pools.
Here's the directions from the AWS Developer Guide to setting up an identity pool.
Once the identity pool is set up, add these permissions to the unauthorized Role your pool is using to allow it access to AWS Cognito ID Pooland
and your database.
With the identity pool set up, we can get to the Java code in our app. Below is an example of code to initialize AmazonDynamoDBClient and DBMapper
for an app.
Let's walk through this code. First, we set up a credential's provider. This is created using the sample code provided from your
Cognito Identity Pool's page in AWS Mobile Hub (shown below). These credentials allow us access to the database (remember the permissions
we added). Then, we set up the DB client using the credentials provided. To fully initialize the Client, it needs to have its Region set
to whatever region your AWS DynamoDB database is stored at (the server location). In the Mobile Hub resources page, under the table name for
each table of your database is listed it's region. Finally, we set up the mapper with our new client.
Step 4: Defining a Mapping Class:
The mapper we created above works to map a class in your application to a table in your database. For every table you want access to,
you'll need a separate mapping class. It is a very simple concept. Using annotations provided by the dynamodbMapper import, we simply create a
class that mimics the structure of the database. Let's look at the code for a mapping class for our Routes table below.
As you can see, this is a very simple class. We just set the attributes that are in the table and provide getters and setters for each.
The only new part is the annotations. When we declare the class, we note the table name. For each attribute, we note the attribute's name
within the table. We even note which attributes are our hash keys and range keys for database queries. The table's hash key is the one you
designated as the partition key when creating the table in AWS. I even show how to note a hash key for a particular index with the userId
attribute, which I use to perform the getRoute's query I'll demonstrate in a bit.
Step 5: Access Your dynamoDB table:
Now we can use our DBmapper with the mapping class to make calls to our database. Below are a few examples with the Routes table. There is more information
provided in the AWS Developer Guide (Part 8: Store and Retreive App Data in Amazon AWS).
Insert/Update with DBmapper save method
Yes, inserts and updates are that simple. Just call the DBmapper's save method with an instance of the mapping class. Not shown is initializing
the Route with the data to be entered into the table (use getter/setter methods).
However, there is a small catch. These database calls cannot be run on the Applications main thread. So, we simply create a new one and run the call
in that.
Querying
Here we set the attribute of the hash key we're using for the query (userId) and set the index to the index to be searched (getRoutes), then
run the query, which returns a PaginatedQueryList of Route objects which match the critera of the QueryExpression that we set. The query on the
database is called with the mapper's query method, which takes in the class-type of the mapping class for the table you're querying and the
query expression. Much more info on setting query expressions can be found in the development guide. The PaginatedQueryList can then be converted to a
List of the mapping class for you to operate on, shown below.
And thats all folks!
© Eric Schreiner 11/17/2016